using System;
using System.Web.UI;
using System.Web.UI.Design;
using System.ComponentModel;
using System.Reflection;
using NBusiness.Data;
using System.Windows.Forms;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;

namespace NBusiness.Data.Web
{

    /// <summary>
    /// A Web Forms data binding control designed to support
    /// NBusiness business objects as data sources.
    /// </summary>
    [Designer(typeof(NBusiness.Data.Web.Design.EntityDataSourceDesigner))]
    [DisplayName("EntityDataSource")]
    [Description("NBusiness Templates Source Control")]
    [ToolboxData("<{0}:EntityDataSource runat=\"server\"></{0}:EntityDataSource>")]
    [ComVisible(false)]
    public class EntityDataSource : DataSourceControl
    {
        public EntityDataSource() 
            : base()
        {
            
        }

        /// <summary>
        /// Event raised when an object is to be created and
        /// populated with data.
        /// </summary>
        /// <remarks>Handle this event in a page and set
        /// e.BusinessObject to the populated business object.
        /// </remarks>
        [Category("Data")]
        [Description("Handle this event to select your business object to data bind against")]
        public event EventHandler<SelectEntityEventArgs> Select;

        /// <summary>
        /// Event raised when an object is to be populated with data
        /// and inserted.
        /// </summary>
        /// <remarks>Handle this event in a page to create an
        /// instance of the object, load the object with data and
        /// insert the object into the database.</remarks>
        [Category("Data")]
        [Description("Handle this event to Insert a data bound business object")]
        public event EventHandler<InsertEntityEventArgs> Insert;

        /// <summary>
        /// Event raised when an object is to be updated.
        /// </summary>
        /// <remarks>Handle this event in a page to update an
        /// existing instance of an object with new data and then
        /// save the object into the database.</remarks>
        [Category("Data")]
        [Description("Handle this event to update a data bound business object")]
        public event EventHandler<UpdateEntityEventArgs> Update;

        /// <summary>
        /// Event raised when an object is to be deleted.
        /// </summary>
        /// <remarks>Handle this event in a page to delete
        /// an object from the database.</remarks>
        [Category("Data")]
        [Description("Handle this event to delete a data bound business object")]
        public event EventHandler<DeleteEntityEventArgs> Delete;

        private EntityDataSourceView _view;
        /// <summary>
        /// Returns the default view for this data control.
        /// </summary>
        /// <param name="viewName">Ignored.</param>
        /// <returns></returns>
        /// <remarks>This control only contains a "Default" view.</remarks>
        protected override DataSourceView GetView(string viewName)
        {
            if(_view==null)
                _view = new EntityDataSourceView(this, viewName);
            return _view;
        }


        private string _assemblyName;
        public string AssemblyName
        {
            get { return _assemblyName; }
            set 
            {
                _assemblyName = value; 
            }
        }

        private string _typeName;
        /// <summary>
        /// Get or set the full type name of the business object
        /// class to be used as a data source.
        /// </summary>
        /// <value>Full type name of the business class.</value>
        public string TypeName
        {
            get { return _typeName; }
            set 
            {
                _typeName = value;
            }
        }

        /// <summary>
        /// Returns a list of views available for this control.
        /// </summary>
        /// <remarks>This control only provides the "Default" view.</remarks>
        protected override System.Collections.ICollection GetViewNames()
        {
            return new string[] { "Default" };
        }

        /// <summary>
        /// Raises the Select event.
        /// </summary>
        internal void OnSelectObject(SelectEntityEventArgs e)
        {
            if (Select != null)
                Select(this, e);
        }

        /// <summary>
        /// Raises the Insert event.
        /// </summary>
        internal void OnInsertObject(InsertEntityEventArgs e)
        {
            if (Insert != null)
                Insert(this, e);
        }

        /// <summary>
        /// Raises the Update event.
        /// </summary>
        internal void OnUpdateObject(UpdateEntityEventArgs e)
        {
            if (Update != null)
                Update(this, e);
        }

        /// <summary>
        /// Raises the Delete event.
        /// </summary>
        internal void OnDeleteObject(DeleteEntityEventArgs e)
        {
            if (Delete != null)
                Delete(this, e);
        }
    }
}
